package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.StudentFaceData;
import com.xmy.cultivate.entity.views.ResStudentFace;
import com.xmy.cultivate.service.IStudentFaceDataService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/studentFaceData")
public class StudentFaceDataController extends BaseController{


    @Autowired
    IStudentFaceDataService iStudentFaceDataService;

    @GetMapping("/getstudentFaceList")
    public Result getstudentFaceList(@RequestParam(value = "pageNum", defaultValue = "1",required = false) Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10",required = false) Integer pageSize,
                                     @RequestParam(value = "schoolId",required = false) String schoolId,
                                     @RequestParam(value = "yearPart",required = false) String yearPart,
                                     @RequestParam(value = "quarterNum",required = false) String quarterNum,
                                     @RequestParam(value = "studentName",required = false) String studentName,
                                     @RequestParam(value = "isSigin",required = false) String isSigin
                                     ){

        IPage<ResStudentFace> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<StudentFaceData> studentFaceDataQueryWrapper = new QueryWrapper<>();
        studentFaceDataQueryWrapper.eq(StringUtils.isNotBlank(yearPart),"year_part",yearPart);
        studentFaceDataQueryWrapper.eq(StringUtils.isNotBlank(quarterNum),"quarter_num",quarterNum);
        //studentFaceDataQueryWrapper.gt("residue_course_count",0);
        studentFaceDataQueryWrapper.inSql(StringUtils.isNotBlank(studentName),"student_id","select id from student where name like '%"+studentName+"%'");

        String schoolStr = schoolId;
        if(StringUtils.isBlank(schoolId)){
            schoolStr = this.baseSchoolIdStr;
        }

        iStudentFaceDataService.getstudentFaceList(iPage,yearPart,quarterNum,schoolStr,isSigin,studentName);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

    @GetMapping("/getClashStudentList")
    public Result getClashStudentList(@RequestParam(value = "pageNum", defaultValue = "1",required = false) Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10",required = false) Integer pageSize,
                                      @RequestParam(value = "schoolId",required = false) String schoolId,
                                      @RequestParam(value = "yearPart",required = false) String yearPart,
                                      @RequestParam(value = "quarterNum",required = false) String quarterNum,
                                      @RequestParam(value = "studentName",required = false) String studentName,
                                      @RequestParam(value = "isEqName",required = false,defaultValue = "0") String isEqName){

        IPage<ResStudentFace> iPage = new Page<>(pageNum, pageSize);
        String schoolStr = schoolId;
        if(StringUtils.isEmpty(schoolId)){
            schoolStr = this.baseSchoolIdStr;
        }
        iStudentFaceDataService.getClashStudentList(iPage,yearPart,quarterNum,schoolStr,studentName,isEqName);

        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }


    /**
     * 删除学生人脸数据
     * @param id
     * @return
     */
    @DeleteMapping("/delFaceDataForId/{id}")
    public Result delFaceDataForId(@PathVariable(value = "id") Long id) throws Exception {
        //StudentFaceData studentFaceData = iStudentFaceDataService.getById(id);
        Integer faceType = 1;//学生为1
        iStudentFaceDataService.delFaceDataForId(id,faceType);

        return new Result(Code.OK,"","");
    }

    /**
     * 删除老师人脸数据
     * @param id
     * @return
     */
    @DeleteMapping("/delFaceDataWithTeacherForId/{id}")
    public Result delFaceDataWithTeacherForId(@PathVariable(value = "id") Long id) throws Exception {
        //StudentFaceData studentFaceData = iStudentFaceDataService.getById(id);
        Integer faceType = 2;//老师为2
        iStudentFaceDataService.delFaceDataForId(id,faceType);
        return new Result(Code.OK,"","");
    }

    /**
     * 更新人脸数据
     * @param studentId
     * @return
     */
    @PutMapping("/updateFaceDataForId/{studentId}")
    public Result updateFaceDataForId(@PathVariable(value = "studentId") Long studentId) throws Exception {
        Integer faceType = 1;//学生为1
        iStudentFaceDataService.updateFaceDataForStudentId(studentId,faceType);
        return new Result(Code.OK,"","");
    }

    /**
     * 更新人脸数据
     * @param studentId
     * @return
     */
    @PutMapping("/updateFaceDataWithTeacherForId/{studentId}")
    public Result updateFaceDataWithTeacherForId(@PathVariable(value = "studentId") Long studentId) throws Exception {
        Integer faceType = 2;//老师为2
        iStudentFaceDataService.updateFaceDataForStudentId(studentId,faceType);
        return new Result(Code.OK,"","");
    }
}
